package com.dinghui.salary.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dinghui.salary.mapper.MouthTypeMapper;
import com.dinghui.salary.mapper.SalaryMapper;
import com.dinghui.salary.pojo.MouthType;
import com.dinghui.salary.pojo.vo.StatisticsVO;
import com.dinghui.salary.pojo.vo.YearMonthStatisticsVO;
import com.dinghui.salary.pojo.vo.YearStatisticsVO;
import com.dinghui.salary.service.SalaryStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tanrentao
 * @date 2021-09-10
 * @desc
 */
@Slf4j
@Service(version = "2.0.0")
public class SalaryStatisticsServiceImpl implements SalaryStatisticsService {

    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private MouthTypeMapper mouthTypeMapper;

    @Override
    public List<StatisticsVO> getSalaryStatisticsByYearAndMonth() {
        Example example = new Example(MouthType.class);
        example.createCriteria().andEqualTo("isDelete", false);
        List<MouthType> list = mouthTypeMapper.selectByExample(example);

        List<StatisticsVO> statisticsVOList = new ArrayList<>();
        list.forEach(mouthType -> {
            StatisticsVO statisticsVO = salaryMapper.selectStatisticsByTableName(mouthType.getTableName());
            if (ObjectUtils.isNotEmpty(statisticsVO)) {
                statisticsVOList.add(statisticsVO);
            }
        });

        List<StatisticsVO> finalList = statisticsVOList.stream()
                .sorted(Comparator.comparing(StatisticsVO::getTime))
                .collect(Collectors.toList());

        return finalList;
    }

    @Override
    public List<YearStatisticsVO> getSalaryStatisticsByYear() {
        Example example = new Example(MouthType.class);
        example.createCriteria().andEqualTo("isDelete", false);
        List<MouthType> list = mouthTypeMapper.selectByExample(example);

        List<YearStatisticsVO> statisticsVOList = new ArrayList<>();
        for (MouthType mouthType : list) {
            StatisticsVO statisticsVO = salaryMapper.selectStatisticsByTableName(mouthType.getTableName());
            if (ObjectUtils.isNotEmpty(statisticsVO)) {
                String mouthTypeYear = mouthType.getTime().substring(0, 4);
                String salaryYear = statisticsVO.getTime().substring(0, 4);
                double yearTotalAmount = 0.0;
                // 年份相等累加
                if (mouthTypeYear.equals(salaryYear)) {
                    yearTotalAmount += Double.parseDouble(statisticsVO.getTotalAmount());
                }
//                YearStatisticsVO yearStatisticsVO = new YearStatisticsVO();
//                yearStatisticsVO.setTotalAmount(yearTotalAmount+"");
//                yearStatisticsVO.setYear(salaryYear);
//                statisticsVOList.add(yearStatisticsVO);
            }
        }

        return statisticsVOList;

        //return salaryMapper.selectSalaryStatisticsByYear();
    }
}
